$(function () {
    findAll();
})

function findAll() {
    let data = {
        username: $("#username").val(),
        phone: $("#phone").val(),
        email: $("#email").val(),
        sex: $("#sex").val(),
        hobby: getHobby(),
        startBirthday: $("#startBirthday").val(),
        endBirthday: $("#endBirthday").val(),
        enable: $('input:radio[name=enable]:checked').val(),
        curPage: $("#curPage").val(),
        pageSize: $("#pageSize").val()

    };
    console.log(data);
    $.ajax({
        url: '/back/user/findAll',
        data: data,
        type: 'get',
        dataType: 'json',
        success: function (data) {
            console.log(data);
            setData(data.mode);
        }
    })
}

function setData(data) {
    let html = '';
    for (let i = 0; i < data.length; i++) {
        html += ' <div>\n' +
            '            <div>' + data[i].id + '</div>\n' +
            '            <div>' + replace(data[i].username) + '</div>\n' +
            '            <div>' + replace(data[i].phone) + '</div>\n' +
            '            <div>' + replace(data[i].email) + '</div>\n' +
            '            <div>' + replace(data[i].birthday) + '</div>\n' +
            '            <div>' + replace(data[i].sex) + '</div>\n' +
            '            <div>' + replace(data[i].hobby) + '</div>\n' +
            '            <div>' + data[i].enable + '</div>\n' +
            '            <div>\n' +
            '                <div><a href="javascript:edit(' + data[i].id + ')"> 编辑</a></div>\n' +
            '                <div onclick="del(' + data[i].id + ')">删除</div>\n' +
            '                <div onclick="passwordEdit(' + data[i].id + ')">密码修改</div>\n' +
            '            </div>\n' +
            '        </div>';
    }
    $(".myBody").html(html);


}
function replace(value) {
    return value==undefined?'':value;
    
}

function del(id) {
    $.ajax({
        url: '/back/user/del',
        data: {id: id},
        dataType: 'json',
        type: 'post',
        success: function (data) {
            console.log(data);
            if (data.result == 'success') {

                findAll();
            } else {
                alert("删除失败");
            }
        }
    })
}
function passwordEdit(id) {
    $("#alertWindow").show();
}
function getHobby() {

    let hobby = '';
    $('input:checkbox[name=hobby]').each(function () {
        if ($(this).prop('checked')) {
            hobby += $(this).val() + ',';
//java.sql.SQLSyntaxErrorException: Unknown column 'form' in 'field list'
            // alert($(this).val());
        }
    });
    if (hobby != '') {
        hobby = hobby.substring(0, hobby.lastIndexOf(','));

    }
    return hobby;

}
function edit(id) {
    $(".right").load("/html/user/userEdit.html");
}